﻿using System;
using System.Collections.Generic;
using System.Text;
using Manager.App_Code.IDAL;
using Manager.App_Code.DTO;
using System.Data;

namespace Manager.App_Code.DAL
{
    class PhieuDangKyDAL:StandardConnectDB, IPhieuDangKy
    {


        public List<DTO.PhieuDangKyPhongDTO> GetListPhieuDK()
        {
            List<PhieuDangKyPhongDTO> listsPhieu = new List<PhieuDangKyPhongDTO>();

            DataTable dt = this.DTExecuteSelect("Select * from PhieuDangKyPhong");

            foreach (DataRow dr in dt.Rows)
            {
                listsPhieu.Add(ConvertObjectDataRowToPhieuDangKyPhongDTO(dr));
            }

            return listsPhieu;
        }

        public int GetMaxMaPhieu()
        {
            DataTable dt = this.DTExecuteSelect("SELECT MAX(MaPhieu) AS MaxMaPhieu FROM dbo.PhieuDangKyPhong");
            string a = dt.Rows[0]["MaxMaPhieu"].ToString();
            if(a!= "")
                return int.Parse(a);
            return 0;
        }

        public int CountPhieuDangKyByMaNhom(long MaNhomKhach)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaNhomKhach", MaNhomKhach);

            DataTable dt = this.DTExecuteSelect("SP_PhieuDangKyPhong_GetAmountTicketByNhomKhach", paramlist);
            string a = dt.Rows[0]["AmountTicket"].ToString();
            if (a != "")
                return int.Parse(a);
            return 0;
        }

        public List<PhieuDangKyPhongDTO> GetListPhieuDKByState(bool State)
        {
            List<PhieuDangKyPhongDTO> listsPhieu = new List<PhieuDangKyPhongDTO>();

            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@State", State);

            DataTable dt = this.DTExecuteSelect("SP_PhieuDangKyPhong_GetByState", paramlist);

            foreach (DataRow dr in dt.Rows)
            {
                listsPhieu.Add(ConvertObjectDataRowToPhieuDangKyPhongDTO(dr));
            }

            return listsPhieu;
        }


        public PhieuDangKyPhongDTO GetPhieuDangKyPhongByMaPhieu(long MaPhieu)
        {
            PhieuDangKyPhongDTO phieudk = new PhieuDangKyPhongDTO();

            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaPhieu", MaPhieu);

            DataTable dt = this.DTExecuteSelect("SP_PhieuDangKyPhong_GetByMaPhieu", paramlist);

            phieudk = ConvertObjectDataRowToPhieuDangKyPhongDTO(dt.Rows[0]);

            return phieudk;

        }

        public List<PhieuDangKyPhongDTO> GetListPhieuDangKyPhongByID(long MaPhieu, string MaPhong)
        {
          
            Dictionary<string, object> paramlist = new Dictionary<string, object>();

           
            List<PhieuDangKyPhongDTO> listsPhieu = new List<PhieuDangKyPhongDTO>();

           
            paramlist.Add("@MaPhieu", MaPhieu);

            paramlist.Add("@MaPhong", MaPhong);

            DataTable dt = this.DTExecuteSelect("SP_PhieuDangKyPhong_GetByID", paramlist);

            foreach (DataRow dr in dt.Rows)
            {
                listsPhieu.Add(ConvertObjectDataRowToPhieuDangKyPhongDTO(dr));
            }

            return listsPhieu;
          
        }

        public PhieuDangKyPhongDTO GetPhieuDangKyPhongByID(long MaPhieu, string MaPhong)
        {
            PhieuDangKyPhongDTO phieudk = new PhieuDangKyPhongDTO();

            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaPhieu", MaPhieu);

            paramlist.Add("@MaPhong", MaPhong);

            DataTable dt = this.DTExecuteSelect("SP_PhieuDangKyPhong_GetByID", paramlist);

            phieudk = ConvertObjectDataRowToPhieuDangKyPhongDTO(dt.Rows[0]);

            return phieudk;
        }


        public bool InsertPhieuDK(DTO.PhieuDangKyPhongDTO phieudk)
        {
             
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            
            paramlist.Add("@MaPhieu", phieudk.MaPhieu);
            
            paramlist.Add("@Phong", phieudk.MaPhong.MaPhong);
            if (phieudk.MaNhomKhach != null)
            {
                paramlist.Add("@MaNhomKhach", phieudk.MaNhomKhach.MaNhomKhach);
            }
            else
            {
                paramlist.Add("@MaNhomKhach", "1");
            }
            paramlist.Add("@MaKhachHang", phieudk.MaKhachHang.MaKH);

            paramlist.Add("@SoNguoi", phieudk.SoNguoi);

            paramlist.Add("@NgayDat", phieudk.NgayDat);
            paramlist.Add("@NgayDen", phieudk.NgayDen);
            paramlist.Add("@NgayDi", phieudk.NgayDi); 
            //paramlist.Add("@NgayTra", phieudk.NgayTra);
            paramlist.Add("@TinhTrang", phieudk.TinhTrang);

            paramlist.Add("@GhiChu", phieudk.GhiChu);

            return this.ExecuteUpdate("SP_PhieuDangKyPhong_Insert", paramlist);
        }


        public bool UpdatePhieuDK(DTO.PhieuDangKyPhongDTO phieudk)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaPhieu", phieudk.MaPhieu);

            paramlist.Add("@Phong", phieudk.MaPhong.MaPhong);
            paramlist.Add("@MaNhomKhach", phieudk.MaNhomKhach.MaNhomKhach);
            
            paramlist.Add("@MaKhachHang", phieudk.MaKhachHang.MaKH);

            paramlist.Add("@SoNguoi", phieudk.SoNguoi);
            paramlist.Add("@NgayDat", phieudk.NgayDat);
            paramlist.Add("@NgayDen", phieudk.NgayDen);
            paramlist.Add("@NgayDi", phieudk.NgayDi);
            paramlist.Add("@NgayTra", phieudk.NgayTra);
            paramlist.Add("@TinhTrang", phieudk.TinhTrang);
            paramlist.Add("@GhiChu", phieudk.GhiChu);

            return this.ExecuteUpdate("SP_PhieuDangKyPhong_Update", paramlist);
        }

        public bool DeletePhieuDK(DTO.PhieuDangKyPhongDTO phieudk)
        {
            throw new NotImplementedException();
        }

        public DTO.PhieuDangKyPhongDTO ConvertObjectDataRowToPhieuDangKyPhongDTO(System.Data.DataRow dr)
        {
            if (dr != null)
            {
                PhieuDangKyPhongDTO phieudk = new PhieuDangKyPhongDTO();
                
                phieudk.MaPhieu = long.Parse(dr["MaPhieu"].ToString());

                IPhong iphong = new PhongDAL();
                phieudk.MaPhong = iphong.GetPhongByID(dr["Phong"].ToString());

                if (dr["MaNhomKhach"].ToString() != "0")
                {
                    INhomKhachDatPhong inhomkhach = new NhomKhachDatPhongDAL();
                    phieudk.MaNhomKhach = inhomkhach.GetNhomKhachByID(dr["MaNhomKhach"].ToString());
                }

                if (dr["MaKhachHang"].ToString() != "")
                {
                    IKhachHang ikhach = new KhachHangDAL();
                    phieudk.MaKhachHang = ikhach.GetKhachHangByID(dr["MaKhachHang"].ToString());

                }

                if (dr["SoNguoi"].ToString() != "")
                    phieudk.SoNguoi= int.Parse(dr["SoNguoi"].ToString());
                

                if (dr["NgayDat"].ToString() != "")
                    phieudk.NgayDat = DateTime.Parse(dr["NgayDat"].ToString());
                
                if (dr["NgayDen"].ToString() != "")
                    phieudk.NgayDen = DateTime.Parse(dr["NgayDen"].ToString());

                if (dr["NgayDi"].ToString() != "")
                    phieudk.NgayDi = DateTime.Parse(dr["NgayDi"].ToString());

                if (dr["NgayTra"].ToString() != "")
                    phieudk.NgayTra = DateTime.Parse(dr["NgayTra"].ToString());

                phieudk.TinhTrang = bool.Parse(dr["TinhTrang"].ToString());

                phieudk.GhiChu = dr["GhiChu"].ToString();

                //phieudk.DatCoc = double.Parse(dr["DatCoc"].ToString());

                //ITienTe itiente = new TienTeDAL();
                //phieudk.DonViTienTe = itiente.GetTienTeByID(dr["DonViTienTe"].ToString());

                return phieudk;
            }
            else
                return null;

            
        }








        public List<PhieuDangKyPhongDTO> GetListPhieuDKByMaNhom(long MaNhom)
        {
            List<PhieuDangKyPhongDTO> listsPhieu = new List<PhieuDangKyPhongDTO>();

            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaNhom", MaNhom);

            DataTable dt = this.DTExecuteSelect("SP_PhieuDangKyPhong_GetByMaNhom", paramlist);
            foreach (DataRow dr in dt.Rows)
            {
                listsPhieu.Add(ConvertObjectDataRowToPhieuDangKyPhongDTO(dr));
            }

            return listsPhieu;
        }












        public PhieuDangKyPhongDTO GetPhieuDangKyPhongByPhongVaTinhTrangDangDuocDat(string MaPhong)
        {
            PhieuDangKyPhongDTO phieudk = new PhieuDangKyPhongDTO();

            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@Phong", MaPhong);

            DataTable dt = this.DTExecuteSelect("SP_PhieuDangKyPhong_GetByPhongAndState", paramlist);

            if (dt.Rows.Count > 0)
            {
                phieudk = ConvertObjectDataRowToPhieuDangKyPhongDTO(dt.Rows[0]);

                return phieudk;
            }
            return null;

        }
    }
}
